﻿using HttpCodeLib;
using System.Collections.Generic;

namespace QQLogin
{
    public class YuMiPlatform : ISmsPlatform
    {
        public string SmsType => "玉米";

        public string UserName { get; set; }
        public string Password { get; set; }
        public string Uid { get; set; }
        public string Token { get; set; }
        public double Balance { get; set; }
        public string AuthorUid { get; set; }
        public int Pid { get; set; }

        public bool NeedTwiceGet => false;

        private readonly Wininet wininet = new Wininet();
        private readonly XJHTTP xjHttp = new XJHTTP();

        public YuMiPlatform()
        {
            AuthorUid = "37956-jyp";
        }

        public void AddIgnoreList(string mobile)
        {
            wininet.GetUtf8($"http://api.jyzszp.com/Api/index/addIgnoreList?uid={UserName}&token={Token}&mobiles={mobile}&pid={Pid}");
        }

        public SmsModel GetCode(string mobile)
        {
            var smsInfo = wininet.GetUtf8($"http://api.jyzszp.com/Api/index/getVcodeAndReleaseMobile?uid={UserName}&token={Token}&mobile={mobile}&pid={Pid}").Split('|');
            if (smsInfo.Length != 3)
            {
                return new SmsModel
                {
                    Success = false,
                    Message = string.Join(":", smsInfo)
                };
            }
            return new SmsModel
            {
                Success = true,
                Data = smsInfo[2]
            };
        }

        public SmsModel GetMobileNum(string mobile = "", string province = "", MobileOperatorEnum _operator = MobileOperatorEnum.任意平台, SmsVno vno = SmsVno.获取任意号段, List<int> section = null)
        {
            var url = $"http://api.jyzszp.com/Api/index/getMobilenum?pid={Pid}&uid={UserName}&token={Token}&size=1";
            if (mobile != "")
            {
                url += $"&mobile={mobile}";
            }
            if (province != "")
            {
                url += $"&varea={xjHttp.UrlEncoding(province)}";
            }
            if (_operator != MobileOperatorEnum.任意平台)
            {
                url += $"&vphoneType={ConvertOperatorEnum(_operator)}";
            }
            if (vno != SmsVno.获取任意号段)
            {
                url += $"&vxnh={ConvertSmsVno(vno)}";
            }
            var mobileInfo = wininet.GetUtf8(url).Split('|');
            if (mobileInfo.Length != 2)
            {
                return new SmsModel
                {
                    Success = false,
                    Message = string.Join(":", mobileInfo)
                };
            }
            if (section != null && section.Count != 0 && !section.Exists(s => mobileInfo[0].StartsWith(s.ToString())))
            {
                AddIgnoreList(mobileInfo[0]);
                return new SmsModel
                {
                    Success = false,
                    Message = $"号段不符合：{mobileInfo[0]}"
                };
            }
            return new SmsModel
            {
                Success = true,
                Data = mobileInfo[0]
            };
        }

        public bool Login(string userName, string password, out string msg)
        {
            msg = "登录成功";
            UserName = userName;
            Password = password;
            var loginInfo = wininet.GetUtf8($"http://api.jyzszp.com/Api/index/userlogin?uid={userName}&pwd={password}").Split('|');
            if (loginInfo.Length != 3)
            {
                msg = $"登录失败：{string.Join(":", loginInfo)}";
                return false;
            }
            Token = loginInfo[2];
            var userInfo = wininet.GetUtf8($"http://api.jyzszp.com/Api/index/getUserInfos?uid={userName}&token={Token}").Split('|');
            if (userInfo.Length != 4)
            {
                msg = $"用户信息获取失败：{string.Join(":", userInfo)}";
                return false;
            }
            if (!double.TryParse(userInfo[1].Replace("余额：", ""), out double balance))
            {
                msg = $"用户信息获取失败：{string.Join(":", userInfo)}";
                return false;
            }
            Balance = balance;
            return true;
        }

        private string ConvertOperatorEnum(MobileOperatorEnum mobileOperator)
        {
            switch (mobileOperator)
            {
                case MobileOperatorEnum.中国电信: return "%E8%81%94%E9%80%9A";
                case MobileOperatorEnum.中国移动: return "%E7%A7%BB%E5%8A%A8";
                case MobileOperatorEnum.中国联通: return "%E7%94%B5%E4%BF%A1";
                default: return "";
            }
        }

        private string ConvertSmsVno(SmsVno smsVno)
        {
            switch (smsVno)
            {
                case SmsVno.只取虚拟号段: return "1";
                case SmsVno.排除虚拟号段: return "2";
                default: return "";
            }
        }
    }
}
